import Mock from "mockjs";

const carsInfo = {
	img: 'https://cdn7.axureshop.com/demo/1972680/images/%E7%BB%84%E4%BB%B6_%E8%A7%84%E8%8C%83/u851.png',
	name: '蒋师傅',
	tel: '180****6666',
	classify: '平板/ 4.2米',
	carsId: '冀AB8888',
	routeList: [{
		start: '石家庄',
		end: '北京',
		isFullChecked: 'true',
		id: '123'
	}, {
		start: '石家庄',
		end: '太原市',
		isFullChecked: 'false',
		id: '33'
	}]
}
let Route = carsInfo.routeList

// 拦截获取司机数据
Mock.mock('/managercars/getcarsInfo', 'post', () => {
	return {
		code: 200,
		message: '司机数据获取成功',
		data: {
			carsInfo
		}
	}
})

// 拦截添加线路接口
Mock.mock('/managercars/addRoute', 'post', (config) => {
	const req = JSON.parse(config.body)
	// console.log(req);
	let id = String(Math.random())
	carsInfo.routeList.push({

		...req,
		id: id
	})



	return {
		code: 200,
		message: '添加路线成功',
	}
})



// 拦截修改线路接口
Mock.mock('/managercars/updateRoute', 'post', (config) => {
	const req = JSON.parse(config.body)
	const index = findIndex(req.id)
	// console.log(oldtarget);
	if (index != -1) {
		carsInfo.routeList[index] = {
			...carsInfo.routeList[index]
			, ...req
		}
		return {
			code: 200,
			message: '修改路线成功',
		}
	} else {
		return {
			code: 200,
			message: '修改路线失败，不存在路线',
		}

	}

})





// 拦截删除线路接口

Mock.mock('/managercars/deleteRoute', 'post', (config) => {
	const req = JSON.parse(config.body)

	carsInfo.routeList = carsInfo.routeList.filter((item) => item.id != req.id)
	return {
		code: 200,
		message: '删除路线成功',
	}
})


// 权限验证功能
function rightCheck(level) {
	switch (level) {
		case 1:
		case 2:
		case 3:

			return true;
		default:
			return false;
	}
}

// 查找数组index
function findIndex(id) {
	return Route.findIndex((item) => item.id == id)


}